<template>
  <el-container>
    <el-header>
      <div>
        <img src="../assets/heima.png"
             alt="">
        <span>后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button"
             @click="toggleCollapse">|||</div>
        <el-menu background-color="#3d3d3c"
                 text-color="#fff"
                 active-text-color="#ffd04b"
                 :collapse="isCollapse"
                 :collapse-transition=false
                 router
                 :default-active="activePath">
          <el-submenu :index="item.id+''"
                      :key="item.id"
                      v-for="item in menuList">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="'/'+item2.path"
                          @click="changeItemActive('/'+item2.path)"
                          :key="item2.id"
                          v-for="item2 in item.children"><i class="el-icon-menu"></i>{{item2.authName}}</el-menu-item>
          </el-submenu>

        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      //   是否是否菜单的变量
      isCollapse: false,
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      activePath: window.sessionStorage.getItem('activePath')
    }
  },
  // 组件的生命周期函数
  created () {
    this.getMenuList()
  },
  methods: {
    // 登出
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧菜单栏数据
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      this.menuList = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
      console.log(this.isCollapse)
    },
    changeItemActive (active) {
      window.sessionStorage.setItem('activePath', active)
      this.activePath = active
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: rgb(121, 118, 118);
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: white;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
  }
  span {
    margin-left: 15px;
  }
}
/deep/.el-aside {
  background-color: rgb(61, 61, 60);
  .el-menu {
    border-right: solid 0 #e6e6e6;
  }
}
.el-main {
  background-color: rgb(255, 255, 255);
}
.toggle-button {
  color: #fff;
  text-align: center;
  // 设置元素内容中各字符之间的边距
  letter-spacing: 0.2em;
  cursor: pointer;
  font-size: 10px;
  height: 28px;
  line-height: 28px;
}
.iconfont {
  margin-right: 10px;
}
</style>
